package com.hz.api.admin.web.app;

import com.github.pagehelper.PageInfo;
import com.hz.api.admin.common.Result.ResultInfo;
import com.hz.api.admin.common.exception.BizException;
import com.hz.api.admin.model.entity.ApiUserPicsEntity;
import com.hz.api.admin.web.service.UserPicsService;
import com.hz.api.admin.web.service.UserService;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/v1")
public class PictureController {

    @Resource
    private UserPicsService userPicsService;

    @Resource
    private UserService userService;

    private static final ThreadPoolExecutor executorService =
            new ThreadPoolExecutor(5, 20, 120, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>(1000), new ThreadPoolExecutor.CallerRunsPolicy());

    @PostMapping("/upload/picture")
    public ResultInfo uploadPicture(@RequestParam("files") MultipartFile[] files) throws InterruptedException {
        userPicsService.uploadPicture(files);
        return ResultInfo.success();
    }

    @GetMapping("/user/picList")
    public ResultInfo getUserPics(HttpServletRequest request) throws InterruptedException {
        String pageSize = request.getParameter("limit");
        String pageNum  = request.getParameter("page");
        PageInfo<ApiUserPicsEntity> list = userPicsService.getUserPics(Integer.valueOf(pageSize), Integer.valueOf(pageNum));
        return ResultInfo.success(list);
    }

    @GetMapping("/download/picture")
    public ResultInfo downloadPicture(HttpServletRequest request) {
        String dirPath = request.getParameter("dirPath");
        userPicsService.downloadPicture(dirPath);
        return ResultInfo.success();
    }

    @GetMapping("/download/process")
    public ResultInfo downloadProcess(HttpServletRequest request) {
        String process = userPicsService.downloadProcess();
        return ResultInfo.buildSuccess(process);
    }

    @GetMapping("/user/picsNum")
    public ResultInfo getUserPicsNum(HttpServletRequest request) {
        Long userPicsNum = userPicsService.getUserPicsNum();
        return ResultInfo.buildSuccess(userPicsNum);
    }

    @PostMapping("/save/picPath")
    public ResultInfo savePicPath(@RequestParam("dirPath") String dirPath) throws InterruptedException {
        userService.updateUser(dirPath);
        return ResultInfo.success();
    }

    @GetMapping("/images")
    public ResponseEntity<org.springframework.core.io.Resource> serveImage(@RequestParam("imageName") String imageName, HttpServletResponse response) throws IOException {
        // 解析出具体文件路径
        File file = new File(imageName);
        // 创建一个文件输入流
        InputStream inputStream = new FileInputStream(file);
        // 将文件流封装为 Spring 的 InputStreamResource
        InputStreamResource resource = new InputStreamResource(inputStream);
        // 构造响应头，指定文件名和内容类型
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
        // 构造 ResponseEntity 返回文件流
        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }
}
